﻿using Seagull.BarTender.Print;
using SharedClientDll;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace AutoPrintLabel
{
    public partial class FormAutoPrintLabel : Form
    {
        private DataTable _data = null;
        private string _btwFile = null;

        public FormAutoPrintLabel()
        {
            InitializeComponent();
        }

        private void ButtonInput_Click(object sender, EventArgs e)
        {
            OpenFileDialog dialog = new OpenFileDialog { ReadOnlyChecked = true };
            dialog.ShowDialog();

            _data = YcdExcelStatic.ExcelToTable(dialog.FileName, out string msg);
            dialog.Dispose();
            Shared.SoftMethod.SoftShared.AppendText(this, this.richTextBoxPrint, msg);
            if (_data != null)
                Shared.SoftMethod.SoftShared.AppendText(this, this.richTextBoxPrint, "数据量为：" + _data.Rows.Count);
        }

        async private void ButtonPrint_Click(object sender, EventArgs e)
        {
            if (_data == null || _data.Rows.Count == 0)
            {
                MessageBox.Show("导入数据为空");
                return;
            }

            int.TryParse(this.textBoxCopies.Text.Trim(), out int copies);
            if(copies<=0 || copies > 3)
            {
                MessageBox.Show("打印份数不正确");
                return;
            }

            this.buttonChoiceBtw.Enabled = false;
            this.buttonInput.Enabled = false;
            this.buttonPrint.Enabled = false;

            Engine bt = new Engine();
            bt.Start();

            LabelFormatDocument document = bt.Documents.Open(_btwFile);
            document.PrintSetup.IdenticalCopiesOfLabel = copies;
            await Task.Run(() =>
            {
                for (int i = 0; i < _data.Rows.Count; i++)
                {
                    document.DatabaseConnections.QueryPrompts["查询提示1"].Value = _data.Rows[i][0].ToString();
                    document.Print();
                    Shared.SoftMethod.SoftShared.AppendText(this, this.richTextBoxPrint, $"打印第[{i}]个标签完成");
                    System.Threading.Thread.Sleep(500);
                }
            });

            _data?.Dispose();
            _data = null;
            _btwFile = null;
            bt.Dispose();

            this.buttonChoiceBtw.Enabled = true;
            this.buttonInput.Enabled = true;
            this.buttonPrint.Enabled = true;
        }

        private void ButtonChoiceBtw_Click(object sender, EventArgs e)
        {
            OpenFileDialog dialog = new OpenFileDialog { ReadOnlyChecked = true };
            dialog.ShowDialog();

            _btwFile = dialog.FileName;
            dialog.Dispose();

            if (_btwFile.Contains(".btw") == false)
            {
                MessageBox.Show("没有选择BTW文件");
                _btwFile = null;
                return;
            }

            MessageBox.Show("选择的文件为：" + _btwFile);
            Shared.SoftMethod.SoftShared.AppendText(this, this.richTextBoxPrint, "选择的文件为：" + _btwFile);
        }

    }
}
